<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategoryList
        @getCategoryId="getCategoryId"
        @clearAttrList="clearAttrList"
        :show="!isShowTable"
      ></CategoryList>
    </el-card>
    <el-card>
      <!-- 属性列表 -->
      <div v-show="isShowTable">
        <!-- 添加属性按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!category3Id"
          @click="showTable"
          >添加属性</el-button
        >
        <!-- 属性列表 -->
        <el-table style="width: 100%" border :data="attrList">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <!-- 属性名称 -->
          <el-table-column prop="attrName" label="属性名称" width="150px">
          </el-table-column>
          <!-- 属性值列表 -->
          <el-table-column prop="prop" label="属性值列表" width="width">
            <template slot-scope="{ row }">
              <el-tag
                type="success"
                v-for="attr in row.attrValueList"
                :key="attr.id"
                style="margin: 0 5px"
                >{{ attr.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column prop="prop" label="操作" width="150px">
            <template slot-scope="{ row }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateValue(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isShowTable">
        <!-- 编辑属性名 -->
        <el-form ref="form" label-width="80px" :inline="true" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input
              placeholder="请输入属性名"
              v-model="attrInfo.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <!-- 添加属性值与取消按钮 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!attrInfo.attrName"
          @click="addValue"
          >添加属性值</el-button
        >
        <el-button @click="isShowTable = true">取消</el-button>
        <!-- 编辑属性列表 -->
        <el-table
          style="width: 100%; margin: 20px 0"
          border
          :data="attrInfo.attrValueList"
        >
          <!-- 序号 -->
          <el-table-column
            type="index"
            align="center"
            label="序号"
            width="80px"
          >
          </el-table-column>
          <!-- 属性值名称 -->
          <el-table-column prop="prop" label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 通过事件委派控制显示与隐藏 -->
              <div @click="focusAttr(row, $index)">
                <el-input
                  v-model="row.valueName"
                  placeholder="请输入属性值名称"
                  size="mini"
                  @keyup.enter.native="tellUserNo2(row, $index)"
                  @blur="tellUserNo(row, $index)"
                  v-if="row.flag"
                  :ref="$index"
                ></el-input>
                <span v-else>{{ row.valueName }}</span>
              </div>
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-popconfirm
                :title="`确定删除${row.valueName}`"
                @onConfirm="deleteAttr($index)"
              >
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  slot="reference"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 保存与取消按钮 -->
        <el-button
          type="primary"
          @click="attrConfirm"
          :disabled="attrInfo.attrValueList.length < 1"
          >保存</el-button
        >
        <el-button @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";

export default {
  name: "Attr",
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      attrList: [],
      //属性列表展示与隐藏
      isShowTable: true,
      //添加属性值与修改
      attrInfo: {
        attrName: "",
        attrValueList: [],
        categoryId: 0,
        categoryLevel: 3,
      },
    };
  },
  methods: {
    //存储id以及发请求获取属性列表
    getCategoryId({ categoryId, level }) {
      if (level === 1) {
        this.category1Id = categoryId;
        this.category2Id = "";
        this.category3Id = "";
      } else if (level === 2) {
        this.category2Id = categoryId;
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        //发请求
        this.getAttrList();
      }
    },
    //获取属性列表
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this;
      let result = await this.$API.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      );
      if (result.code === 200) {
        this.attrList = result.data;
      }
    },
    //点击添加属性按钮显示与隐藏
    showTable() {
      this.isShowTable = false;
      this.attrInfo = {
        attrName: "",
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 3,
      };
    },
    //点击编辑按钮显示与隐藏 修改 为每一个添加flag属性
    updateValue(row) {
      this.isShowTable = false;
      this.attrInfo = cloneDeep(row);
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, "flag", false);
      });
    },
    //点击添加属性值
    addValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: "",
        flag: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus();
      });
    },
    //清除属性列表
    clearAttrList() {
      this.attrList = [];
    },
    //失去焦点提示用户
    tellUserNo(row, index) {
      if (row.valueName.trim() == "") {
        this.attrInfo.attrValueList.splice(index, 1);
        this.$message.error("输入值不能为空！");
        return;
      } else {
        row.flag = false;
      }
      let isRepat = this.attrInfo.attrValueList.some((item) => {
        if (row !== item) {
          return row.valueName == item.valueName;
        }
      });
      if (isRepat) {
        // this.attrInfo.attrValueList.splice(index, 1);
        this.$message.error("输入值重复，请重新添加！");
        row.valueName = "";
        row.flag = true;
        this.$refs[index].focus();
      } else {
        row.flag = false;
      }
    },
    //键盘回车提示用户
    tellUserNo2(row, index) {
      if (row.valueName.trim() == "") {
        this.attrInfo.attrValueList.splice(index, 1);
        this.$message.error("输入值不能为空！");
        return;
      } else {
        row.flag = false;
      }
      let isRepat = this.attrInfo.attrValueList.some((item) => {
        if (row !== item) {
          return row.valueName == item.valueName;
        }
      });
      if (isRepat) {
        // this.attrInfo.attrValueList.splice(index, 1);
        this.$message.error("输入值重复，请重新添加！");
        row.valueName = "";
        row.flag = true;
      } else {
        row.flag = false;
      }
    },
    //点击span获取焦点并span消失，表单显示
    focusAttr(row, index) {
      row.flag = true;
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    //点击删除按钮 删除对应属性
    deleteAttr(index) {
      this.attrInfo.attrValueList.splice(index, 1);
    },
    //点击保存发请求  删除flag属性
    async attrConfirm() {
      let result = this.attrInfo.attrValueList.every((item) => {
        return item.valueName !== "";
      });

      if (result) {
        this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(
          (item) => {
            delete item.flag;
            return true;
          }
        );
        //发请求
        try {
          await this.$API.attr.reqAddOrUpdateList(this.attrInfo);
          this.isShowTable = true;
          this.$message({
            type: "success",
            message: "保存成功",
          });
          this.getAttrList();
        } catch (error) {
          this.$message.error(error);
        }
      } else {
        this.attrInfo.attrValueList.forEach((item) => {
          if (item.valueName == "") {
            item.flag = true;
          }
        });
      }
    },
  },
};
</script>

<style>
</style>